using System;
using System.Collections.Generic;
using System.Text;

namespace AMPLibrary.Media
{
    public class MediaTypes
    {
        public const string PlaylistTypeFilter = "Playlist File(s)|*.m3u;*.M3U;|All files (*.*)|*.*";

        public const string MediaTypeFilter = "Media File(s)|*.mp3;*.avi;|All files (*.*)|*.*";

        public const string MusicMediaTypeFilter = "Music Media File(s)|*.mp3;|All files (*.*)|*.*";

        public const string VideoMediaTypeFilter = "Video Media File(s)|*.avi;|All files (*.*)|*.*";

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static bool IsValidMediaType(string filename)
        {
            bool isValidType = false;

            switch (GetMimeType(filename))
            {
                case MimeTypes.AVI:
                    isValidType = true;
                    break;

                case MimeTypes.MP3:
                    isValidType = true;
                    break;

                case MimeTypes.M3U:
                case MimeTypes.M3Ux:
                    isValidType = true;
                    break;
            }

            return isValidType;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static bool IsValidMusicMediaType(string filename)
        {
            bool isValidType = false;

            switch (GetMimeType(filename))
            {
                case MimeTypes.MP3:
                    isValidType = true;
                    break;
            }

            return isValidType;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static bool IsValidVideoMediaType(string filename)
        {
            bool isValidType = false;

            switch (GetMimeType(filename))
            {
                case MimeTypes.AVI:
                    isValidType = true;
                    break;
            }

            return isValidType;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static bool IsValidPlaylistMediaType(string filename)
        {
            bool isValidType = false;

            switch (GetMimeType(filename))
            {
                case MimeTypes.M3U:
                case MimeTypes.M3Ux:
                    isValidType = true;
                    break;

                case MimeTypes.WPL:
                    isValidType = true;
                    break;
            }

            return isValidType;
        }

        /// <summary>
        /// Taken from Kaushik Chakraborti
        /// http://kseesharp.blogspot.com/2008/04/c-get-mimetype-from-file-name.html
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string GetMimeType(string fileName)
        {
            string mimeType = "application/unknown";

            try
            {
                string ext = System.IO.Path.GetExtension(fileName).ToLower();
                Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);

                if (regKey != null && regKey.GetValue("Content Type") != null)
                    mimeType = regKey.GetValue("Content Type").ToString();
            }
            catch
            {
            }

            return mimeType;
        }
    }
}
